package com.sijiao.controller;


import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.google.json.JsonSanitizer;
import com.sijiao.pojo.ProductMangementPojo;
import com.sijiao.service.ReceiveDataService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

/**
 * 接收数据
 *
 * @author ShiLian
 * @date 2022/07/17 12:55
 **/
@Controller
@RequestMapping("/receive")
public class ReceiveData {

    private static final Logger log = LoggerFactory.getLogger(ReceiveData.class);

    @Autowired
    private ReceiveDataService receiveDataService;

    /**
     * 主动去调用接口拉取参数
     *
     * @param params
     */
    @RequestMapping(path = "/receiveByInterface", method = RequestMethod.POST)
    public void receiveByInterface(@RequestBody String params) {
        Gson gson = new Gson();
        try {
            String santize = JsonSanitizer.sanitize(params);
            List<ProductMangementPojo> models = new Gson().fromJson(santize, new TypeToken<List<ProductMangementPojo>>() {
            }.getType());
            receiveDataService.ReceiveData(models);
        } catch (Exception e) {
            log.error("ReceiveData receiveByInterface is Exception!" + e);
        }
    }
}
